<script setup>
import { Edit, Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import {
  getCourseByTeacherIdService,

} from '../../api/course'


import { useUserInfoStore } from '../../stores/userInfo'
const userInfoStore = useUserInfoStore()
const router = useRouter()
let data = userInfoStore.userInfo
const userInfo = ref({
  ...data,
})

const courses = ref([])

import {getTeacherByIdService} from '../../api/teacher'
const searchQuery = ref('') // 新增的搜索条件

const filteredCourses = computed(() => {
  return courses.value/* .filter(course =>
    course.name.includes(searchQuery.value)
  ) */
})
// 弹框标题
const name = ref('')
//控制添加课程弹窗
const dialogVisible = ref(false)
//添加课程数据模型

// 课程表单模型
const courseModel = ref({
  name: '',
  credit: '',
  teacherId:'',
  teacherName: '',
  isOpen:'',
  id: 0,
})


// 显示课程列表信息
const courseList = async () => {
  const teacherId = userInfo.value.id;
  let res = await getCourseByTeacherIdService(teacherId);
  const coursesData = res.data;
  const teacherPromises = coursesData.map(async (course) => {
    const teacherResponse = await getTeacherByIdService(course.teacherId);
    course.teacherName = teacherResponse.data.name; // 添加教师名字
    return course;
  });
  
  courses.value = await Promise.all(teacherPromises); // 等待所有请求完成
}
const showStudent = (id) => { 
  router.push({ path: '/teacherCourse/student', query: { couId: id } }); //携带id查询
}

courseList()
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>您的课程</span>
        <el-input 
          v-model="searchQuery" 
          placeholder="搜索相关课程..." 
          style="width: 200px; margin-right: 10px;" 
        />
      </div>
    </template>
    <!-- 列表 -->
    <el-table :data="filteredCourses"  style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="课程" prop="name"></el-table-column>
      <el-table-column label="学分" prop="credit"></el-table-column>
      <el-table-column label="授课教师" prop="teacherName"></el-table-column>
      <el-table-column label="开课状态" prop="isOpen">
        <template #default="{ row }">
        <span>{{ row.isOpen === 1 ? '开课' : '未开课' }}</span>
         </template>
      </el-table-column>
      <el-table-column label="选课学生">
        <template #default="{ row }">
          <el-button  plain type="success" @click="showStudent(row.id)">查看学生</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
  </el-card>
</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
